package com.example.platosreports;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class KI extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ki);
		
		Intent intent = getIntent();
		String fileInput = intent.getStringExtra(DailySearch.THIS_DATE);
		
		try {
			kiInput(fileInput);
		} catch (IOException e) {
			//TODO Auto0generated catch block
			e.printStackTrace();
		}
		
		//Up Action on Action Bar
		setupActionBar();
	}
	
	
//Function that searches text file and prints out data to screen
	private void kiInput( String fileInput) throws IOException
	{
		String fileName = fileInput + ".txt";
		File inFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName);
		
		//KI Variables
		String accGPM = null;
		String accSales = null;
		String accMMU = null;
		String accOnHnd = null;
		String mbGPM = null;
		String mbSales = null;
		String mbMMU = null;
		String mbOnHnd = null;
		String mfwGPM = null;
		String mfwSales = null;
		String mfwMMU = null;
		String mfwOnHnd = null;
		String mssGPM = null;
		String mssSales = null;
		String mssMMU = null;
		String mssOnHnd = null;
		String wbGPM = null;
		String wbSales = null;
		String wbMMU = null;
		String wbOnHnd = null;
		String wfwGPM = null;
		String wfwSales = null;
		String wfwMMU = null;
		String wfwOnHnd = null;		
		String wssGPM = null;
		String wssSales = null;
		String wssMMU = null;
		String wssOnHnd = null;		
		
		
		
		if(inFile.exists())
		{
			FileInputStream tryThis = new FileInputStream(inFile);
			BufferedReader myReader =  new BufferedReader(new InputStreamReader(tryThis));
			
			String data2 = null; //hold input string
			int flag = 1;  //Flag to get out of loop when value is found
			String [] inputArr;
			
			
			//Gets to correct section
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("DEPARTMENT:"))
				{
					if(inputArr[1].equals("ALL"))
					{
						if(inputArr[2].equals("COMBINED"))
						{
							//This report has multiple sections, this while loop gets me to the correct section
							flag = -999;
						}
						else
						{
							flag = 1;
						}
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}
			
			//Gets Accessories info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("10:ACCESSORIES"))
				{
					accGPM = inputArr[3];
					accSales = inputArr[4];
					accMMU = inputArr[7];
					accOnHnd = inputArr[8];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			//Gets Mens Basic info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("20:MENS"))
				{
					mbGPM = inputArr[4];
					mbSales = inputArr[5];
					mbMMU = inputArr[8];
					mbOnHnd = inputArr[9];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			//Gets Mens f/w info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("30:MENS"))
				{
					mfwGPM = inputArr[4];
					mfwSales = inputArr[5];
					mfwMMU = inputArr[8];
					mfwOnHnd = inputArr[9];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			//Gets Mens s/s info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("40:MENS"))
				{
					mssGPM = inputArr[4];
					mssSales = inputArr[5];
					mssMMU = inputArr[8];
					mssOnHnd = inputArr[9];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			//Gets Womens basic info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("50:WOMENS"))
				{
					wbGPM = inputArr[4];
					wbSales = inputArr[5];
					wbMMU = inputArr[8];
					wbOnHnd = inputArr[9];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			//Gets Womens f/w info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("60:WOMENS"))
				{
					wfwGPM = inputArr[4];
					wfwSales = inputArr[5];
					wfwMMU = inputArr[8];
					wfwOnHnd = inputArr[9];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			//Gets Womens s/s info
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("70:WOMENS"))
				{
					wssGPM = inputArr[4];
					wssSales = inputArr[5];
					wssMMU = inputArr[8];
					wssOnHnd = inputArr[9];
					flag = -999;
				}	
				else
				{
					flag = 1;
				}
			}
			
			
			//Print out all variables
			
			
			
			
			
			
			//String rows = ("Category \u0020 \u0020 \u0020 GPM% \u0020 \u0020 \u0020 % of Sales \u0020 \u0020 \u0020 MMU% \u0020 \u0020 \u0020 % On Hand");
			TextView showAccGpm = (TextView) findViewById(R.id.accGpm);
			showAccGpm.setText(accGPM);
			TextView showMbGpm = (TextView) findViewById(R.id.mbGpm);
			showMbGpm.setText(mbGPM);
			TextView showMfwGpm = (TextView) findViewById(R.id.mfwGpm);
			showMfwGpm.setText(mfwGPM);
			TextView showMssGpm = (TextView) findViewById(R.id.mssGpm);
			showMssGpm.setText(mssGPM);
			TextView showWbGpm = (TextView) findViewById(R.id.wbGpm);
			showWbGpm.setText(wbGPM);
			TextView showWfwGpm = (TextView) findViewById(R.id.wfwGpm);
			showWfwGpm.setText(wfwGPM);
			TextView showWssGpm = (TextView) findViewById(R.id.wssGpm);
			showWssGpm.setText(wssGPM);
			
			TextView showAccSales = (TextView) findViewById(R.id.accSales);
			showAccSales.setText(accSales);
			TextView showMbSales = (TextView) findViewById(R.id.mbSales);
			showMbSales.setText(mbSales);
			TextView showMfwSales = (TextView) findViewById(R.id.mfwSales);
			showMfwSales.setText(mfwSales);
			TextView showMssSales = (TextView) findViewById(R.id.mssSales);
			showMssSales.setText(mssSales);
			TextView showWbSales = (TextView) findViewById(R.id.wbSales);
			showWbSales.setText(wbSales);
			TextView showWfwSales = (TextView) findViewById(R.id.wfwSales);
			showWfwSales.setText(wfwSales);
			TextView showWssSales = (TextView) findViewById(R.id.wssSales);
			showWssSales.setText(wssSales);
			
			TextView showAccMMU = (TextView) findViewById(R.id.accMMU);
			showAccMMU.setText(accMMU);
			TextView showMbMMU = (TextView) findViewById(R.id.mbMMU);
			showMbMMU.setText(mbMMU);
			TextView showMfwMMU = (TextView) findViewById(R.id.mfwMMU);
			showMfwMMU.setText(mfwMMU);
			TextView showMssMMU = (TextView) findViewById(R.id.mssMMU);
			showMssMMU.setText(mssMMU);
			TextView showWbMMU = (TextView) findViewById(R.id.wbMMU);
			showWbMMU.setText(wbMMU);
			TextView showWfwMMU = (TextView) findViewById(R.id.wfwMMU);
			showWfwMMU.setText(wfwMMU);
			TextView showWssMMU = (TextView) findViewById(R.id.wssMMU);
			showWssMMU.setText(wssMMU);
			
			TextView showAccOnHnd = (TextView) findViewById(R.id.accOnHnd);
			showAccOnHnd.setText(accOnHnd);
			TextView showMbOnHnd = (TextView) findViewById(R.id.mbOnHnd);
			showMbOnHnd.setText(mbOnHnd);
			TextView showMfwOnHnd = (TextView) findViewById(R.id.mfwOnHnd);
			showMfwOnHnd.setText(mfwOnHnd);
			TextView showMssOnHnd = (TextView) findViewById(R.id.mssOnHnd);
			showMssOnHnd.setText(mssOnHnd);
			TextView showWbOnHnd = (TextView) findViewById(R.id.wbOnHnd);
			showWbOnHnd.setText(wbOnHnd);
			TextView showWfwOnHnd = (TextView) findViewById(R.id.wfwOnHnd);
			showWfwOnHnd.setText(wfwOnHnd);
			TextView showWssOnHnd = (TextView) findViewById(R.id.wssOnHnd);
			showWssOnHnd.setText(wssOnHnd);
			
			//String showAcc = ("Accessories \u0020" + accGPM + "\u0020" + accSales + "\u0020" + accMMU + "\u0020" + accOnHnd);
			//TextView disAcc = (TextView) findViewById(R.id.accView);
			//disAcc.setText(showAcc);
			/*String showMb = ("Men's Basic \u0020" + mbGPM + "\u0020" + mbSales + "\u0020" + mbMMU + "\u0020" + mbOnHnd);
			TextView disMb = (TextView) findViewById(R.id.mbView);
			disMb.setText(showMb);
			String showMss= ("Men's F/W \u0020" + mssGPM + "\u0020" + mssSales + "\u0020" + mssMMU + "\u0020" + mssOnHnd);
			TextView disMss = (TextView) findViewById(R.id.mfwView);
			disMss.setText(showMss);
			String showMfw = ("Men's S/S \u0020" + mfwGPM + "\u0020" + mfwSales + "\u0020" + mfwMMU + "\u0020" + mfwOnHnd);
			TextView disMfw = (TextView) findViewById(R.id.mssView);
			disMfw.setText(showMfw);
			String showWb = ("Women's Basic \u0020" + wbGPM + "\u0020" + wbSales + "\u0020" + wbMMU + "\u0020" + wbOnHnd);
			TextView disWb = (TextView) findViewById(R.id.wbView);
			disWb.setText(showWb);
			String showWfw = ("Women's F/W \u0020" + wfwGPM + "\u0020" + wfwSales + "\u0020" + wfwMMU + "\u0020" + wfwOnHnd);
			TextView disWfw = (TextView) findViewById(R.id.wfwView);
			disWfw.setText(showWfw);
			String showWss = ("Women's S/S \u0020" + wssGPM + "\u0020" + wssSales + "\u0020" + wssMMU + "\u0020" + wssOnHnd);
			TextView disWss = (TextView) findViewById(R.id.wssView);
			disWss.setText(showWss);
			*/
			
			myReader.close();
		}
		
		else
		{
			TextView testData2 = (TextView) findViewById(R.id.fileNotFound);
			testData2.setText("File Not Found");
		}
	}
	
	
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.ki, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			Intent intent1 = new Intent(this, PlatosReports.class);
			intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent1);
			
		}
		return super.onOptionsItemSelected(item);
	}


}


